package com.weixin1.servlet;

import com.weixin1.bean.Users;
import com.weixin1.dto.R;
import com.weixin1.service.UserService;
import com.weixin1.utils.JSONUtil;
import com.weixin1.utils.MyBeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/userservlet/*")
public class UserServlet extends HttpServlet {
    UserService us=new UserService();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String PathInfo = request.getPathInfo().substring(1);
        if("unamecount".equals(PathInfo)){
            unamecount(request,response);
        }else if("umessagecount".equals(PathInfo)){
            umessagecount(request,response);
        }else if("save".equals(PathInfo)){
            save(request,response);
        }else if("list".equals(PathInfo)){
            selectByPage(request,response);
        }else if("removeById".equals(PathInfo)){
            removeById(request,response);
        }else if("update".equals(PathInfo)){
            update(request,response);
        }else if("content".equals(PathInfo)){
            content(request,response);
        }else if("utime".equals(PathInfo)){
            utime(request,response);
        }
    }

    //根据预约时间查询预约个数
    private void utime(HttpServletRequest request, HttpServletResponse response) {
        try {
            long utime = us.utime();
            request.getSession().setAttribute("utime",utime);
            response.getWriter().print(JSONUtil.toString(utime));
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //首页留言管理 查询出名字时间和留言内容
    private void content(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            List<Users> users = us.selectContent();
            Map<String,Object> map=new HashMap<>();
            map.put("list",users);
            response.getWriter().print(JSONUtil.toString(map));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //修改数据
    private void update(HttpServletRequest request, HttpServletResponse response) throws IOException {

        try {
//            long uId = Long.valueOf(request.getParameter("uId"));
//            String uName = request.getParameter("uName");
//            String uGender = request.getParameter("uGender");
//            int uAge = Integer.parseInt(request.getParameter("uAge"));
//            String uBirthday = request.getParameter("uBirthday");
//            String uEmail = request.getParameter("uEmail");
//            String uMessage = request.getParameter("uMessage");
//
//            Users user=new Users();
//            user.setUId(uId);
//            user.setUName(uName);
//            user.setUAge(uAge);
//            user.setUGender(uGender);
//            user.setUEmail(uEmail);
//            user.setUBirthday(new SimpleDateFormat("yyyy-MM-dd").parse(uBirthday));
//            user.setUMessage(uMessage);
            Users users = MyBeanUtils.reqToBean(request, Users.class);

            boolean update = us.update(users);
            if (update){
                response.getWriter().print(JSONUtil.toString(R.ok()));
            }else {
                response.getWriter().print(JSONUtil.toString(R.error()));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //删除数据
    private void removeById(HttpServletRequest request, HttpServletResponse response) {
        Long uId = Long.valueOf(request.getParameter("id"));
        try {
            boolean b = us.removeById(uId);
            if(b){
                response.getWriter().print(JSONUtil.toString(R.ok()));
            }else{
                response.getWriter().print(JSONUtil.toString(R.error()));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //查询所有列表
    private void selectByPage(HttpServletRequest request, HttpServletResponse response) {
        Integer pageNumber = Integer.valueOf(request.getParameter("page"));
        Integer pageSize = Integer.valueOf(request.getParameter("limit"));
//        String searchText = request.getParameter("searchText");
//        String sortName = request.getParameter("sortName");
//        String sortOrder = request.getParameter("sortOrder");
//        String searchText = request.getParameter("searchText");
//        String sortName = request.getParameter("sortName");
//        String sortOrder = request.getParameter("sortOrder");

        try {
            List<Users> list = us.selectByPage(pageNumber,pageSize);
            int total =us.gettotal();

            Map<String,Object> map=new HashMap<>();
            map.put("code",0);
            map.put("count",total);
            map.put("data",list);


            response.getWriter().print(JSONUtil.toString(map));
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //更新用户，添加
    private void save(HttpServletRequest request, HttpServletResponse response) {

        try {
//            String uName = request.getParameter("uName");
//            String uGender = request.getParameter("uGender");
//            int uAge = Integer.valueOf(request.getParameter("uAge"));
//            String uBirthday = request.getParameter("uBirthday");
//            String uMessage = request.getParameter("uMessage");
//            String uEmail = request.getParameter("uEmail");
//
//
//            Users user1=new Users();
//            user1.setUName(uName);
//            user1.setUAge(uAge);
//            user1.setUGender(uGender);
//            user1.setUEmail(uEmail);
//            user1.setUBirthday(new SimpleDateFormat("yyyy-MM-dd").parse(uBirthday));
//            user1.setUMessage(uMessage);
            Users users = MyBeanUtils.reqToBean(request, Users.class);

            boolean b=us.save(users);
            if(b){
                response.getWriter().print(JSONUtil.toString(R.ok()));
            }else{
                response.getWriter().print(JSONUtil.toString(R.error()));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //根据留言属性获取留言个数
    private void umessagecount(HttpServletRequest request, HttpServletResponse response) {
        try {
            long umessagecount =us.umessagecount();
            request.getSession().setAttribute("umessagecount",umessagecount);
            response.getWriter().write(JSONUtil.toString(umessagecount));

        } catch (SQLException e) {

            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //根据用户名获取用户个数
    private void unamecount(HttpServletRequest request, HttpServletResponse response) {
        try {
            long unamecount = us.unamecount();
            request.getSession().setAttribute("unamecount",unamecount);
            response.getWriter().write(JSONUtil.toString(unamecount));

        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
